/*
 * Created by JFormDesigner on Thu Jun 27 10:45:16 CST 2024
 */

package scct.jiangjiale.ui;

import scct.jiangjiale.dao.UserDao;

import java.awt.*;
import java.awt.event.*;
import java.awt.font.TextHitInfo;
import java.sql.SQLException;
import javax.swing.*;

/**
 * @author 蒋家乐
 */
public class DigUserAdd extends JDialog {
    public DigUserAdd(Window owner) {
        super(owner);
        initComponents();
    }

    private void add(ActionEvent e) throws SQLException {
        // TODO add your code here
        String name = txtName.getText();
        String password = txtPassword.getText();
        int age = Integer.parseInt(txtAge.getModel().getValue().toString());
        String sex = man.isSelected() ? "男" : "女";
        String mobile = phoneNumber.getText();
        if (!name.matches("^\\w{3,8}$")) {
            JOptionPane.showMessageDialog(this, "请输入合法的用户名");
            return;
        }
        if (age < 10 || age > 100) {
            JOptionPane.showMessageDialog(this, "请输入正确的年龄");
            return;
        }
        //       if (!mobile.matches())
        UserDao userDao = new UserDao();
        if (userDao.addUser(name, password, sex, age, mobile)) {
            JOptionPane.showMessageDialog(this, "添加成功");
        }else {
            JOptionPane.showMessageDialog(this,"添加失败");
        }
        this.dispose();

    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 蒋家乐
        label1 = new JLabel();
        panel1 = new JPanel();
        panel2 = new JPanel();
        txtName = new JTextField();
        txtPassword = new JTextArea();
        label2 = new JLabel();
        label3 = new JLabel();
        phoneNumber = new JTextField();
        label4 = new JLabel();
        txtAge = new JSpinner();
        label5 = new JLabel();
        man = new JRadioButton();
        woman = new JRadioButton();
        buttonAdd = new JButton();
        buttonnot = new JButton();

        //======== this ========
        setTitle("\u6dfb\u52a0\u7528\u6237");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7528\u6237\u540d");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(40, 45), label1.getPreferredSize()));

        //======== panel1 ========
        {
            panel1.setBorder ( new javax . swing. border .CompoundBorder ( new javax . swing. border .TitledBorder ( new javax . swing. border .
            EmptyBorder ( 0, 0 ,0 , 0) ,  "JF\u006frmD\u0065sig\u006eer \u0045val\u0075ati\u006fn" , javax. swing .border . TitledBorder. CENTER ,javax . swing
            . border .TitledBorder . BOTTOM, new java. awt .Font ( "Dia\u006cog", java .awt . Font. BOLD ,12 ) ,
            java . awt. Color .red ) ,panel1. getBorder () ) ); panel1. addPropertyChangeListener( new java. beans .PropertyChangeListener ( )
            { @Override public void propertyChange (java . beans. PropertyChangeEvent e) { if( "\u0062ord\u0065r" .equals ( e. getPropertyName () ) )
            throw new RuntimeException( ) ;} } );
            panel1.setLayout(null);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(new Rectangle(new Point(105, 40), panel1.getPreferredSize()));

        //======== panel2 ========
        {
            panel2.setLayout(null);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel2.getComponentCount(); i++) {
                    Rectangle bounds = panel2.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel2.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel2.setMinimumSize(preferredSize);
                panel2.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel2);
        panel2.setBounds(100, 40, panel2.getPreferredSize().width, 0);
        contentPane.add(txtName);
        txtName.setBounds(90, 35, 200, txtName.getPreferredSize().height);
        contentPane.add(txtPassword);
        txtPassword.setBounds(95, 90, 190, txtPassword.getPreferredSize().height);

        //---- label2 ----
        label2.setText("\u5bc6\u7801");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(45, 95), label2.getPreferredSize()));

        //---- label3 ----
        label3.setText("\u624b\u673a\u53f7\u7801");
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(25, 145), label3.getPreferredSize()));
        contentPane.add(phoneNumber);
        phoneNumber.setBounds(90, 135, 200, 35);

        //---- label4 ----
        label4.setText("\u5e74\u9f84");
        contentPane.add(label4);
        label4.setBounds(new Rectangle(new Point(45, 195), label4.getPreferredSize()));
        contentPane.add(txtAge);
        txtAge.setBounds(90, 185, 200, 35);

        //---- label5 ----
        label5.setText("\u6027\u522b");
        contentPane.add(label5);
        label5.setBounds(new Rectangle(new Point(45, 235), label5.getPreferredSize()));

        //---- man ----
        man.setText("\u7537");
        contentPane.add(man);
        man.setBounds(new Rectangle(new Point(100, 230), man.getPreferredSize()));

        //---- woman ----
        woman.setText("\u5973");
        contentPane.add(woman);
        woman.setBounds(new Rectangle(new Point(225, 230), woman.getPreferredSize()));

        //---- buttonAdd ----
        buttonAdd.setText("\u6dfb\u52a0");
        buttonAdd.addActionListener(e -> add(e));
        contentPane.add(buttonAdd);
        buttonAdd.setBounds(new Rectangle(new Point(80, 280), buttonAdd.getPreferredSize()));

        //---- buttonnot ----
        buttonnot.setText("\u53d6\u6d88");
        contentPane.add(buttonnot);
        buttonnot.setBounds(new Rectangle(new Point(230, 280), buttonnot.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(545, 370);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 蒋家乐
    private JLabel label1;
    private JPanel panel1;
    private JPanel panel2;
    private JTextField txtName;
    private JTextArea txtPassword;
    private JLabel label2;
    private JLabel label3;
    private JTextField phoneNumber;
    private JLabel label4;
    private JSpinner txtAge;
    private JLabel label5;
    private JRadioButton man;
    private JRadioButton woman;
    private JButton buttonAdd;
    private JButton buttonnot;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
